#ifndef __SYS_H
#define __SYS_H

#include "stm32f10x.h"
#include <stdlib.h>
#include <stdio.h>

#define BIT_SET(REG, BIT)     ((REG) |= (1 << (BIT)))
#define BIT_CLEAR(REG, BIT)   ((REG) &= ~(1 << (BIT)))
#define BIT_READ(REG, BIT)    ((REG) & (1 << (BIT)))
#define REG_MODIFY(REG, MASK, VAL)  ((REG) = ((REG) & ~(MASK)) | (VAL))

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) 

#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 

//IO口操作,只对单一的IO口
//确保n的值小于16
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //

/* STM32大容量产品每页大小2KByte，中、小容量产品每页大小1KByte */
#if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
  #define FLASH_PAGE_SIZE       ((uint16_t)0x800)	// 2048
#else
  #define FLASH_PAGE_SIZE       ((uint16_t)0x400)	// 1024
#endif

#define WRITE_START_ADDR        ((uint32_t)0x0800FC00)  //flash写入起始地址
#define WRITE_END_ADDR          ((uint32_t)0x08010000)  //flash写入结束地址

typedef union 
{
    uint8_t  u8[4];
    uint16_t u16[2];
    uint32_t u32;
} dataTypeChange_t;

char* HexToString(uint16_t hex);
char* IntToString(int num);
char* FloatToString(float num);
void BitToString(uint8_t data, char* array);
uint8_t ADD8_Calculation(uint8_t *array, uint8_t length);
uint8_t ADD8_Check(uint8_t *array, uint8_t length);

//Flash相关
void Flash_Init(void);
void Flash_Write(uint32_t address, uint16_t* data, uint16_t length);
void Flash_Read(uint32_t address, uint16_t* data, uint16_t length);

#endif
